package com.andcard.action;

import com.andcard.AndCard;
import com.andcard.R;
import com.andcard.obj.Card;
import com.andcard.sql.DbAdapter;
import com.andcard.util.Reference;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TabArrange extends AbsAction implements OnClickListener{

	private TextView entryInsertQuestion;
	private TextView entryInsertAnswer;
	private Button btInsert;
	private TextView entryUpdateQuestion;
	private TextView entryUpdateAnswer;
	private Button btUpdate;
	
	private int idx = -1;
	
	public TabArrange(AndCard activity, DbAdapter dbAdapter){
		
		super(activity, dbAdapter);
		
		Log.d(Reference.TAG, "TabArrange.Constructor");
		
		entryInsertQuestion = (TextView)activity.findViewById(R.id.entryInsertQuestion);
		entryInsertAnswer = (TextView)activity.findViewById(R.id.entryInsertAnswer);
		btInsert = (Button)activity.findViewById(R.id.btInsert);
		
		entryUpdateQuestion = (TextView)activity.findViewById(R.id.entryUpdateQuestion);
		entryUpdateAnswer = (TextView)activity.findViewById(R.id.entryUpdateAnswer);
		btUpdate = (Button)activity.findViewById(R.id.btUpdate);
		
		btInsert.setOnClickListener(this);
		btUpdate.setOnClickListener(this);
	}
	
	public void onClick(View v) {
		
		Log.d(Reference.TAG, "TabArrange.onClick - " + v.toString() );
		
        // Perform action on click
		if (v.equals(btInsert)){
			insertRow();
		}else if(v.equals(btUpdate)){
			updateRow();
		}
	}
	
	public void activeTab(int counter){
		
		Log.d(Reference.TAG, "TabArrange.activeTab -counter " + counter );
		
		this.entryUpdateQuestion.setText("");
		this.entryUpdateAnswer.setText("");
		
		if (this.getDbAdapter().isDatabaseOpen() == false || counter < 0 || counter >= this.getDbAdapter().getListCard().size()){
			// no message
			return;
		}
		
		Card card = this.getDbAdapter().getCard(counter);
		
		if (card == null){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_STATS);
			return;
		}
		
		idx = counter;
		this.entryUpdateQuestion.setText(card.getQuestion());
		this.entryUpdateAnswer.setText(card.getAnswer());
	}
	
	public void updateRow(){
		
		Log.d(Reference.TAG, "TabArrange.activeTab -updateRow" );
		
		if (this.getDbAdapter().isDatabaseOpen() == false){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_OPEN);
			return;
		}
		
		if (this.getDbAdapter().isDatabaseReadOnly()){
			this.getActivity().showDialog(Reference.ALERT_DATABASE_READ_ONLY);
			return;
		}
		
		if (this.entryUpdateQuestion.getText() == null || this.entryUpdateQuestion.getText().toString().trim().equals("") ||
			this.entryUpdateAnswer.getText() == null || this.entryUpdateAnswer.getText().toString().trim().equals("")
			){
				this.getActivity().showDialog(Reference.ALERT_NO_UPDATE_DATA);
				return;
		}
		
		if (this.getDbAdapter().updateCardData(idx, this.entryUpdateQuestion.getText().toString().trim(), this.entryUpdateAnswer.getText().toString().trim())){
			this.getActivity().showDialog(Reference.ALERT_OK_UPDATE);
		}else{
			this.getActivity().showDialog(Reference.ALERT_KO_UPDATE);
		}
	}
	
	public void insertRow(){
		
		Log.d(Reference.TAG, "TabArrange.activeTab -insertRow" );
		
		if (this.getDbAdapter().isDatabaseOpen() == false){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_OPEN);
			return;
		}

		if (this.getDbAdapter().isDatabaseReadOnly()){
			this.getActivity().showDialog(Reference.ALERT_DATABASE_READ_ONLY);
			return;
		}
		
		if (this.entryInsertQuestion.getText() == null || this.entryInsertQuestion.getText().toString().trim().equals("") ||
				this.entryInsertAnswer.getText() == null || this.entryInsertAnswer.getText().toString().trim().equals("")
				){
					this.getActivity().showDialog(Reference.ALERT_NO_INSERT_DATA);
					return;
		}

		if (this.getDbAdapter().insertCard(this.entryInsertQuestion.getText().toString().trim(), this.entryInsertAnswer.getText().toString().trim())){
			this.getActivity().showDialog(Reference.ALERT_OK_INSERT);
			this.entryInsertQuestion.setText("");
			this.entryInsertAnswer.setText("");
		}else{
			this.getActivity().showDialog(Reference.ALERT_KO_INSERT);
		}	
	}
}
